package com.fzl.springblogdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzl.springblogdemo.common.exception.BlogException;
import com.fzl.springblogdemo.mapper.UserInfoMapper;
import com.fzl.springblogdemo.pojo.dataobject.BlogInfo;
import com.fzl.springblogdemo.pojo.dataobject.UserInfo;
import com.fzl.springblogdemo.pojo.request.UserLoginRequest;
import com.fzl.springblogdemo.pojo.request.UserRegisterRequest;
import com.fzl.springblogdemo.pojo.response.UserInfoResponse;
import com.fzl.springblogdemo.pojo.response.UserLoginResponse;
import com.fzl.springblogdemo.util.BeanTransUtils;
import com.fzl.springblogdemo.util.JwtUtils;
import com.fzl.springblogdemo.util.SecurityUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    @Override
    public UserLoginResponse checkPassword(UserLoginRequest userLoginRequest) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getUserName, userLoginRequest.getUserName())
                .eq(UserInfo::getDeleteFlag, 0);
        //查询数据库
        //TODO 添加try catch
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if (userInfo==null){
            //用户不存在
            throw new BlogException("请填写正确的用户名");
        }
        //判断密码是否正确
//        if (!userLoginRequest.getPassword().equals(userInfo.getPassword())){
//            throw new BlogException("用户密码错误");
//        }
        if (!SecurityUtil.verify(userLoginRequest.getPassword(), userInfo.getPassword())){
            throw new BlogException("用户密码错误");
        }
        //密码正确
        Map<String, Object> map = new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("name", userInfo.getUserName());
        String token = JwtUtils.genToken(map);
        return new UserLoginResponse(userInfo.getId(), token);
    }


    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfo::getDeleteFlag,0)
                .eq(UserInfo::getId,userId);
        UserInfo userInfo=userInfoMapper.selectOne(queryWrapper);
        return BeanTransUtils.trans(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
       //根据blogId获取博客信息
        BlogInfo blogInfo=blogService.getBolgInfo(blogId);
        if(blogInfo==null||blogInfo.getUserId()<0){
            throw new BlogException("博客不存在");
        }
        //根据zuozheID，获取作者信息
        return getUserInfo(blogInfo.getUserId());

    }

    @Override
    public Boolean register(UserRegisterRequest userRegisterRequest) {
        UserInfo userInfo=new UserInfo();
        BeanUtils.copyProperties(userRegisterRequest,userInfo);
        try{
            userInfo.setPassword(SecurityUtil.encrypt(userRegisterRequest.getPassword()));
            Integer result=userInfoMapper.insert(userInfo);
            if(result==1){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("用户注册失败："+e);
            throw new BlogException("用户注册失败，内部错误，请联系管理员");
        }
    }
}
